package com.itqf.Controller;


import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Shippingaddress.ShippingaddressService;
import com.itqf.Service.User.UserService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.SuccessMessages;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.Shippingaddress;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShippingaddressController {
@Resource
private UserService userService;
@Resource
private ShippingaddressService shippingaddressService;
   /*
   查询个人的收货地址
   李朝银
    */
    @GetMapping("/huileme/a/u/ShippingaddressController/shippingaddress")
    @ResponseBody()
    public ResultDTO<List> findShippingaddress(HttpServletRequest request){
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid = getheader.getUserid();
        /*User user = userService.findbytoken(token);
        String userid = user.getUserid();
*/
        ResultDTO resultDTO=new ResultDTO();
        List<Shippingaddress> shippingaddressList = shippingaddressService.findShippingaddress(userid);


        if (!shippingaddressList.isEmpty())
        {
            resultDTO.setData(shippingaddressList);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(shippingaddressList);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }


        return resultDTO;
    }
    /*
    编辑收货地址回显
    李朝银
     */
    @GetMapping("/huileme/a/u/ShippingaddressController/shippingaddress1")
    @ResponseBody()
    public ResultDTO<Shippingaddress> findbyShippingaddressid(Integer shippingid){
        Shippingaddress shippingaddress = shippingaddressService.findbyShippingaddressid(shippingid);

        ResultDTO resultDTO=new ResultDTO();
        if (shippingaddress!=null)
        {
            resultDTO.setData(shippingaddress);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(shippingaddress);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }
        return resultDTO;
    }

    /*
    修改收货地址
    李朝银
     */
    @PostMapping("/huileme/a/u/ShippingaddressController/updetshippingAddress")
    @ResponseBody()
    public ResultDTO updetShippingaddressid(Integer shippingid, String shippingpeople,
                                                  String shippingphone, String region, String address){

        Map map =new HashMap();
        map.put("shippingid",shippingid);
        map.put("shippingpeople",shippingpeople);
        map.put("shippingphone",shippingphone);
        map.put("region",region);
        map.put("address",address);
        ResultDTO resultDTO=new ResultDTO();
        int i = shippingaddressService.updetShippingaddressid(map);
        if (i>0){
          resultDTO.setCode(CodeEnum.Updatesuccess.getCode());
          resultDTO.setMessage(CodeEnum.Updatesuccess.getMsg());

        }else {
            resultDTO.setCode(CodeEnum.Updateerror.getCode());
            resultDTO.setMessage(CodeEnum.Updateerror.getMsg());
        }
        return resultDTO;
    }
    /*
    添加收货地址
    李朝银
     */
    @PostMapping("/huileme/a/u/ShippingaddressController/addshippingAddress")
    @ResponseBody()
    public ResultDTO addShippingaddressid(HttpServletRequest request,String shippingpeople, String shippingphone,String region,String address){
        /*User user = userService.findbytoken(token);
        String userid = user.getUserid();*/

        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid = getheader.getUserid();
        Map map =new HashMap();
        map.put("userid",userid);
        map.put("shippingpeople",shippingpeople);
        map.put("shippingphone",shippingphone);
        map.put("region",region);
        map.put("address",address);
        int i = shippingaddressService.addShippingaddressid(map);
        ResultDTO resultDTO=new ResultDTO();
        if (i>0){
            resultDTO.setCode(CodeEnum.addsuccess.getCode());
            resultDTO.setMessage(CodeEnum.addsuccess.getMsg());
        }else
        {
            resultDTO.setCode(CodeEnum.adderror.getCode());
            resultDTO.setMessage(CodeEnum.adderror.getMsg());
        }
       return resultDTO;
    }
    /*
    删除收货地址
    李朝银
     */
    @DeleteMapping("/huileme/a/u/ShippingaddressController/deleteid")
    @ResponseBody()
    public ResultDTO deleteShippingaddressid(Integer shippingid){
        int i = shippingaddressService.deleteShippingaddressid(shippingid);
        ResultDTO resultDTO=new ResultDTO();
        if (i>0){
            resultDTO.setCode(CodeEnum.deletesuccess.getCode());
            resultDTO.setMessage(CodeEnum.deletesuccess.getMsg());
        }else {
            resultDTO.setCode(CodeEnum.deleteerror.getCode());
            resultDTO.setMessage(CodeEnum.deleteerror.getMsg());
        }

        return resultDTO;
    }
}
